package com.qqtech.qquan.coupon.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.community.model.CommunityBaseQuery;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.coupon.enums.CouponBaseBizzTypeDictEnum;
import com.qqtech.qquan.coupon.model.CouponBase;
import com.qqtech.qquan.coupon.model.CouponBaseQuery;
import com.qqtech.qquan.coupon.model.CouponBaseVo;
import com.qqtech.qquan.coupon.service.CouponBaseService;
import com.qqtech.qquan.homemaking.model.HomemakingTypeQuery;
import com.qqtech.qquan.homemaking.model.HomemakingTypeVo;
import com.qqtech.qquan.homemaking.service.HomemakingTypeService;
import com.qqtech.qquan.mall.enums.MallConst;
import com.qqtech.qquan.mall.enums.MallProductStatusDictEnum;
import com.qqtech.qquan.mall.model.MallCategoryQuery;
import com.qqtech.qquan.mall.model.MallCategoryVo;
import com.qqtech.qquan.mall.model.MallProductQuery;
import com.qqtech.qquan.mall.model.MallProductVo;
import com.qqtech.qquan.mall.model.MallShopQuery;
import com.qqtech.qquan.mall.model.MallShopVo;
import com.qqtech.qquan.mall.service.MallCategoryService;
import com.qqtech.qquan.mall.service.MallProductService;
import com.qqtech.qquan.mall.service.MallShopService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.sys.enums.SysConst;

@Controller
@RequestMapping(value = "/coupon/base")
public class CouponBaseController extends BaseAdminController {
	@Resource
	private CouponBaseService couponBaseService;

	@Resource
	private MallProductService mallProductService;

	@Resource
	private MallShopService mallShopService;

	@Resource
	private MallCategoryService mallCategoryService;

	@Resource
	private HomemakingTypeService homemakingTypeService;

	@Resource
	private CommunityBaseService communityBaseService;

	@Override
	protected BaseService<CouponBase> getBaseService() {
		return couponBaseService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/coupon/base-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(CouponBaseQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<CouponBaseVo> page = couponBaseService.queryPageList(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	@RequestMapping(value = "/detail")
	public String detail(Integer id, Model model) {
		CouponBaseVo detailVo = couponBaseService.getById(id);
		String category = "";
		if (detailVo.getBizzType() == CouponBaseBizzTypeDictEnum.HOME_MAKING_TYPE.getCode()) {// 服务类目
			HomemakingTypeVo homemakingTypeVo = homemakingTypeService.getById(detailVo.getCategoryId());
			category = homemakingTypeVo.getName();
		} else if (detailVo.getBizzType() == CouponBaseBizzTypeDictEnum.MALL_CATEGORY.getCode()) {// 商城类目
			MallCategoryVo mallCategoryVo = mallCategoryService.getById(detailVo.getCategoryId());
			category = mallCategoryVo.getName();
		} else if (detailVo.getBizzType() == CouponBaseBizzTypeDictEnum.MALL_PRODUCT.getCode()) {// 商城商品
			MallProductVo mallProductVo = mallProductService.getById(detailVo.getBizzId());
			category = mallProductVo.getName();
		}
		model.addAttribute("category", category);
		model.addAttribute("detailVo", detailVo);
		return "/coupon/base-detail";
	}

	@RequestMapping(value = "/getMallProductList")
	@ResponseBody
	public Result getMallProductList(MallProductQuery query, Model model) {
		Result ret = new Result();
		query.setStatus(MallProductStatusDictEnum.ONSHELF.getCode());
		Pageable pageKit = new PageKit(query.getPage(), Integer.MAX_VALUE, new PageOrder(Direction.DESC, "id"));
		List<MallProductVo> list = mallProductService.queryList(query, pageKit);
		for (MallProductVo mallProductVo : list) {
			mallProductVo.setImgString(mallProductVo.getImg(), MallConst.IMG_SMALL);
		}
		ret.put("list", list);
		return ret;
	}

	@RequestMapping(value = "/getMallCategoryList")
	@ResponseBody
	public Result getMallCategoryList(MallCategoryQuery query, Model model) {
		Result ret = new Result();
		query.setStatus(YesNoEnum.YES.getCode());
		query.setLevel(1);
		Pageable pageKit = new PageKit(query.getPage(), Integer.MAX_VALUE, new PageOrder(Direction.DESC, "id"));
		List<MallCategoryVo> list = mallCategoryService.queryList(query, pageKit);
		for (MallCategoryVo mallCategoryVo : list) {
			mallCategoryVo.setImgString(mallCategoryVo.getImg(), SysConst.IMG_POSTFIX);
		}
		ret.put("list", list);
		return ret;
	}

	@RequestMapping(value = "/getSecondHomemakingTypeList")
	@ResponseBody
	public Result getHomemakingTypeList(HomemakingTypeQuery query, Model model) {
		Result ret = new Result();
		query.setStatus(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(query.getPage(), Integer.MAX_VALUE, new PageOrder(Direction.DESC, "id"));
		List<HomemakingTypeVo> list = homemakingTypeService.queryList(query, pageKit);
		for (HomemakingTypeVo homemakingTypeVo : list) {
			homemakingTypeVo.setImgString(homemakingTypeVo.getImg(), SysConst.IMG_POSTFIX);
		}
		ret.put("list", list);
		return ret;
	}

	/**
	 * 创建成功
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(CouponBase addBase) {
		Result ret = new Result();
		addBase.setStatus(YesNoEnum.NO.getCode());
		couponBaseService.insert(addBase);
		return ret;
	}

	/**
	 * 创建成功
	 * 
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Result update(CouponBase updateBase) {
		Result ret = new Result();
		couponBaseService.updateIsNull(updateBase.getId());
		couponBaseService.updateById(updateBase);
		return ret;
	}

	/**
	 * 启用
	 * 
	 * @return
	 */
	@RequestMapping(value = "/up")
	@ResponseBody
	public Result up(Integer id) {
		Result ret = new Result();
		CouponBase updateBase = new CouponBase();
		updateBase.setId(id);
		updateBase.setStatus(YesNoEnum.YES.getCode());
		couponBaseService.updateById(updateBase);
		return ret;
	}

	/**
	 * 停止
	 * 
	 * @return
	 */
	@RequestMapping(value = "/qBuyDown")
	@ResponseBody
	public Result qBuyDown(Integer id) {
		Result ret = new Result();
		CouponBase updateBase = new CouponBase();
		updateBase.setId(id);
		updateBase.setIsQBuy(YesNoEnum.NO.getCode());
		couponBaseService.updateById(updateBase);
		return ret;
	}

	/**
	 * 启用
	 * 
	 * @return
	 */
	@RequestMapping(value = "/qBuyUp")
	@ResponseBody
	public Result qBuyUp(Integer id) {
		Result ret = new Result();
		CouponBase updateBase = new CouponBase();
		updateBase.setId(id);
		updateBase.setIsQBuy(YesNoEnum.YES.getCode());
		couponBaseService.updateById(updateBase);
		return ret;
	}

	/**
	 * 停止
	 * 
	 * @return
	 */
	@RequestMapping(value = "/down")
	@ResponseBody
	public Result down(Integer id) {
		Result ret = new Result();
		CouponBase updateBase = new CouponBase();
		updateBase.setId(id);
		updateBase.setStatus(YesNoEnum.NO.getCode());
		couponBaseService.updateById(updateBase);
		return ret;
	}

	/**
	 * 去新增
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		HomemakingTypeQuery query = new HomemakingTypeQuery();
		query.setStatus(YesNoEnum.YES.getCode());
		query.setParentId(0);
		Pageable pageKit = new PageKit(query.getPage(), Integer.MAX_VALUE, new PageOrder(Direction.DESC, "id"));
		List<HomemakingTypeVo> homemakingTypes = homemakingTypeService.queryList(query, pageKit);
		model.addAttribute("homemakingTypes", homemakingTypes);

		HomemakingTypeQuery query2 = new HomemakingTypeQuery();
		query2.setStatus(YesNoEnum.YES.getCode());
		query2.setParentId(homemakingTypes.get(0).getId());
		List<HomemakingTypeVo> homemakingTypes2 = homemakingTypeService.queryList(query2, pageKit);
		model.addAttribute("homemakingTypes2", homemakingTypes2);

		MallShopQuery mallShopQuery = new MallShopQuery();
		mallShopQuery.setStatus(YesNoEnum.YES.getCode());
		mallShopQuery.setCommunityId(0);// 商城
		List<MallShopVo> mallList = mallShopService.queryList(mallShopQuery);
		Integer shopId = -1;
		if (null != mallList && mallList.size() > 0) {
			shopId = mallList.get(0).getId();
		}
		CommunityBaseQuery communityBaseQuery = new CommunityBaseQuery();
		communityBaseQuery.setIsOpen(YesNoEnum.YES.getCode());
		communityBaseQuery.setIsShow(YesNoEnum.YES.getCode());
		List<CommunityBaseVo> communityList = communityBaseService.queryList(communityBaseQuery);
		model.addAttribute("communityList", communityList);
		model.addAttribute("shopId", shopId);
		model.addAttribute("mallList", mallList);
		return "/coupon/base-add";
	}

	@RequestMapping(value = "/getCMallList")
	@ResponseBody
	public Result getCMallList(MallShopQuery query, Model model) {
		Result ret = new Result();
		query.setStatus(YesNoEnum.YES.getCode());
		List<MallShopVo> list = mallShopService.queryList(query);
		Integer shopId = -1;
		if (null != list && list.size() > 0) {
			shopId = list.get(0).getId();
		}
		ret.put("list", list);
		ret.put("shopId", shopId);
		return ret;
	}

	/**
	 * 去编辑
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/toUpdate")
	public String toUpdate(Integer id, Model model) {
		CouponBaseVo detailVo = couponBaseService.getById(id);
		model.addAttribute("detailVo", detailVo);
		HomemakingTypeQuery query = new HomemakingTypeQuery();
		query.setStatus(YesNoEnum.YES.getCode());
		query.setParentId(0);
		Pageable pageKit = new PageKit(query.getPage(), Integer.MAX_VALUE, new PageOrder(Direction.DESC, "id"));
		List<HomemakingTypeVo> homemakingTypes = homemakingTypeService.queryList(query, pageKit);
		model.addAttribute("homemakingTypes", homemakingTypes);
		Integer parentId = homemakingTypes.get(0).getId();
		if (detailVo.getBizzType() == CouponBaseBizzTypeDictEnum.HOME_MAKING_TYPE.getCode()) {
			HomemakingTypeVo detailVoHomeMakingTpye = homemakingTypeService.getById(detailVo.getCategoryId());
			parentId = detailVoHomeMakingTpye.getParentId();
			model.addAttribute("detailVoHomeMakingTpye", detailVoHomeMakingTpye);
		}

		HomemakingTypeQuery query2 = new HomemakingTypeQuery();
		query2.setStatus(YesNoEnum.YES.getCode());
		query2.setParentId(parentId);
		List<HomemakingTypeVo> homemakingTypes2 = homemakingTypeService.queryList(query2, pageKit);
		model.addAttribute("homemakingTypes2", homemakingTypes2);
		
		CommunityBaseQuery communityBaseQuery = new CommunityBaseQuery();
		communityBaseQuery.setIsOpen(YesNoEnum.YES.getCode());
		communityBaseQuery.setIsShow(YesNoEnum.YES.getCode());
		List<CommunityBaseVo> communityList = communityBaseService.queryList(communityBaseQuery);
		model.addAttribute("communityList", communityList);
		
		if(detailVo.getCommunityId()!=null){
			//如果适用社区id 不为空
			model.addAttribute("communityId", detailVo.getCommunityId());
		}

		if (detailVo.getBizzType() == CouponBaseBizzTypeDictEnum.MALL_PRODUCT.getCode()) {
			MallProductVo mallProductVo2 = mallProductService.getById(detailVo.getBizzId());
			MallShopVo mallShopVo = mallShopService.getById(mallProductVo2.getShopId());

			MallProductQuery mallQuery = new MallProductQuery();
			mallQuery.setStatus(MallProductStatusDictEnum.ONSHELF.getCode());
			mallQuery.setShopId(mallShopVo.getId());
			Pageable pageKit2 = new PageKit(query.getPage(), Integer.MAX_VALUE, new PageOrder(Direction.DESC, "id"));
			List<MallProductVo> mallProductList = mallProductService.queryList(mallQuery, pageKit2);
			for (MallProductVo mallProductVo : mallProductList) {
				mallProductVo.setImgString(mallProductVo.getImg(), MallConst.IMG_SMALL);
			}
			model.addAttribute("mallProductList", mallProductList);

			MallShopQuery mallShopQuery = new MallShopQuery();
			mallShopQuery.setStatus(YesNoEnum.YES.getCode());
			mallShopQuery.setCommunityId(mallShopVo.getCommunityId());// 商城
			List<MallShopVo> mallList = mallShopService.queryList(mallShopQuery);
			
			model.addAttribute("shopId", mallShopVo.getId());
			model.addAttribute("mallList", mallList);
			model.addAttribute("communityId", mallShopVo.getCommunityId());
		}
		if (detailVo.getBizzType() == CouponBaseBizzTypeDictEnum.SHOP.getCode()) {
			Integer communityId = detailVo.getCommunityId();
			MallShopVo mallShopVo = mallShopService.getById(detailVo.getBizzId());
			if(communityId==null||communityId.intValue()==0){
				if(mallShopVo!=null&&mallShopVo.getCommunityId()!=null){
					communityId = mallShopVo.getCommunityId();
				}
			}
			MallShopQuery mallShopQuery = new MallShopQuery();
			mallShopQuery.setStatus(YesNoEnum.YES.getCode());
			mallShopQuery.setCommunityId(communityId);// 商城
			List<MallShopVo> mallList = mallShopService.queryList(mallShopQuery);
			model.addAttribute("mallList", mallList);
			model.addAttribute("shopId", mallShopVo.getId());
			model.addAttribute("communityId", communityId);
		}
		return "/coupon/base-update";
	}
}
